package com.itentbase.match.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author:wjl
 * @create: 2024-01-15 14:12
 * @Description: 格式化工具类
 */
@Slf4j
public class FormatUtils {

    //支持2024-06-04,2024-6-4,2024-11-12
    public static final DateTimeFormatter FORMATTER_DATE = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter FORMATTER_DATE2 = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    public static final DateTimeFormatter FORMATTER_DATE3 = DateTimeFormatter.ofPattern("MM-dd HH:mm");
    /**
     * 日期对象转换为字符串
     *
     * @param localDate
     * @return {@link String }
     * @author wjl
     * @date 2024-01-15
     **/
    public static String format(LocalDate localDate) {
        if (localDate == null)
            return null;
        try {
            return localDate.format(FORMATTER_DATE);
        } catch (Exception e) {
            log.error("com.itentbase.lottery.common.utils.FormatUtils.format,localDate=" + localDate);
            log.error("error:", e);
        }
        return null;
    }

    /**
     * 字符串转换为日期对象
     *
     * @param src
     * @return {@link LocalDate }
     * @author wjl
     * @date 2024-01-22
     **/
    public static LocalDate parse(String src) {
        if (src == null)
            return null;
        try {
            return LocalDate.parse(src, FORMATTER_DATE);
        } catch (Exception e) {
            log.error("com.itentbase.lottery.common.utils.FormatUtils.parse,src=" + src);
            log.error("error:", e);
        }
        return null;
    }

    public static LocalDateTime longToLocalDateTime(long timestamp) {
        if (timestamp <= 0)
            return null;
        try {
            return LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
        } catch (Exception e) {
            log.error("com.itentbase.lottery.common.utils.FormatUtils.timestampToLocalDateTime,timestamp=" + timestamp);
            log.error("error:", e);
        }
        return null;
    }

    /**
     * LocalDate To Date
     *
     * @param localDate
     * @return {@link Date }
     * @author wjl
     * @date 2024-01-22
     **/
    public static Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }


    /**
     * 根据彩票规则进行四舍五入
     *
     * @param result
     * @return {@link BigDecimal }
     * @author wjl
     * @date 2024-02-02
     **/
    public static BigDecimal lotteryHalfAdjust(BigDecimal result) {
        //去掉小数点3位后面的数据
        result = result.setScale(3, RoundingMode.DOWN);
        //获取第3位小数
        int thirdDigit = result.movePointRight(3).intValue() % 10;
        //获取第2位小数
        int secondDigit = result.movePointRight(2).intValue() % 10;

        if (thirdDigit >= 0 && thirdDigit <= 4) {
            // 第3位数字为0\~4时，小数点第2位以后数字直接舍去
            result = result.setScale(2, RoundingMode.DOWN);
        } else if (thirdDigit >= 6 && thirdDigit <= 9) {
            // 第3位数字为6\~9时，小数点第2位数字加1，其后数字舍去
            result = result.setScale(2, RoundingMode.UP);
        } else if (thirdDigit == 5) {
            if (secondDigit % 2 == 0) {
                // 第3位数字为5且第2位数字为偶数时，小数点第2位以后数字直接舍去
                result = result.setScale(2, RoundingMode.DOWN);
            } else {
                // 第3位数字为5且第2位数字为奇数时，小数点第2位数字加1，其后数字舍去
                result = result.setScale(2, RoundingMode.UP);
            }
        }
        return result;
    }


    /**
     * 将字符串类型的时间和日期拼接后转为时间类型
     *
     * @param dateString
     * @param timeString
     * @return
     */
    public static LocalDateTime getDateSplicing(String dateString, String timeString) {
        return LocalDateTime.parse(dateString + " " + timeString,
                FORMATTER_DATE2);

    }

    //秒转换为LocalDate
    public static LocalDate longToLocalDate(long timestamp) {
        Instant instant = Instant.ofEpochSecond(timestamp);
        return instant.atZone(ZoneId.systemDefault()).toLocalDate();
    }
    //LocalDate转换为秒的long
    public static long LocalDateToLong(LocalDate localDate) {
        Instant instant = localDate.atStartOfDay(ZoneId.systemDefault()).toInstant();
        return instant.getEpochSecond();
    }

    public static Integer stringToInteger(String src){
        if (StringUtils.hasLength(src)){
            try {
                src=src.replaceAll(",",""); //去掉货币逗号
                return Integer.valueOf(src);
            }catch (Exception e){}
        }
        return null;
    }

    /**
     * 05-29 18:00 转换为当前年月日时间 2024-06-04T18:00
     * @param dayTime
     * @return {@link LocalDateTime }
     * @author wjl
     * @date 2024-06-04
     **/
    public static LocalDateTime stringToLocalDateTime(String dayTime){
        if (StringUtils.isEmpty(dayTime)){
            return null;
        }
        if (dayTime.length()>11){
            return LocalDateTime.parse(dayTime,FORMATTER_DATE2);
        }else{
            return LocalDateTime.of(LocalDate.now(),
                    LocalTime.parse(dayTime, FORMATTER_DATE3));
        }
    }
}
